import { TOKEN_KEY } from '@/constants'
import type { Profile } from '@/types/user'
import Cookies from 'js-cookie'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('hm-user', () => {
  const token = ref(Cookies.get(TOKEN_KEY) || '')
  const setToken = (value: string) => {
    // 存储 token 到 store 中，内存访问速度更快
    token.value = value
    // 通过 Cookies 存储 token（后台管理系统较为常见）
    Cookies.set(TOKEN_KEY, value)
  }
  // 移除 token
  const removeToken = () => {
    // 内存中删除
    token.value = ''
    // 本地中删除
    Cookies.remove(TOKEN_KEY)
  }

  // 登陆后获取个人信息
  const profile = ref<Profile>({} as Profile)
  const setProfile = (value: Profile) => {
    profile.value = value
  }
  const removeProfile = () => {
    profile.value = {} as Profile
  }
  return { token, setToken, removeToken, profile, setProfile, removeProfile }
})
